<template>
  <div class="mainBox">
    <van-nav-bar
      title="登录"
      left-text="返回"
      left-arrow
      @click-left="$router.back(-1)"
    />
    <!-- 手机号验证码输入框 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="phone"
        name="mobile"
        label="手机号"
        placeholder="请输入手机号"
        :rules="[
          { required: true, message: '请填写手机号' },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '手机号格式不对',
          },
        ]"
      />
      <van-field
        v-model="password"
        name="code"
        label="验证码"
        placeholder="请输入验证码"
        :rules="[
          { required: true, message: '请填写验证码' },
          { pattern: /\d{6}/, message: '验证码格式不对' },
        ]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          :disabled="loading"
          :loading="loading"
          loading-text="加载中..."
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { validateLogon } from "./user";
import { settoKen } from "../../utils/token";
export default {
  name: "login",
  data() {
    return {
      phone: "",
      password: "",
      loading:false,
    };
  },
  methods: {
    async onSubmit(values) {
      try {
          let res =  await validateLogon({
            mobile: values.mobile,
            code: values.code,
          });
          this.loading = true
          setTimeout(() => {
          this.$toast.success("登录成功");
          settoKen(res.data.data);
          this.$router.push({ path: "/" });
        }, 1000);
        
      } catch (error) {
        console.log(error);
        this.$toast.success("账号或验证码错误");
      }
    },
  },
};
</script>

<style lang="less">
.mainBox {
  .van-nav-bar__content {
    background-image: linear-gradient(to right bottom, red, yellow);
    .van-nav-bar__title {
      color: white;
    }
    .van-nav-bar__text {
      color: white;
    }
    .van-nav-bar__arrow {
      color: white;
    }
  }
}
</style>